//
//  MaintenanceTopView.m
//  carcareIOS
//
//  Created by ileo on 15/9/2.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "ZigzagView.h"

#define GAP 4

@interface ZigzagView()

@end

@implementation ZigzagView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.type = Type_Zigzag_Sharp;
    }
    return self;
}

-(void)setType:(Type_Zigzag)type{
    _type = type;
    [self setNeedsDisplay];
}

-(void)drawRect:(CGRect)rect{
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);
    CGContextSaveGState(context);
    
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, 0.5);
    CGContextSetStrokeColorWithColor(context, [Theme lineColor].CGColor);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    
    switch (self.type) {
        case Type_Zigzag_Sharp:
        {
            CGContextMoveToPoint(context, 0, self.height);
            CGContextAddLineToPoint(context, 0, 0);
            CGContextAddLineToPoint(context, self.width, 0);
            CGContextAddLineToPoint(context, self.width, self.height);
            
            NSInteger num = 120;
            CGFloat width = self.width / num;
            
            for (int i = 0; i <= num; i++) {
                CGFloat sub = (i % 2 == 0 ? 0 : GAP);
                CGContextAddLineToPoint(context, self.width - width*i, self.height - sub);
            }
        }
            break;
            
        case Type_Zigzag_Round:
        {
            NSInteger num = 20;
            CGFloat width = self.width / num / 3;
            
            CGContextMoveToPoint(context, 0, self.height - width);
            CGContextAddLineToPoint(context, 0, 0);
            CGContextAddLineToPoint(context, self.width, 0);
            CGContextAddLineToPoint(context, self.width, self.height - width);
            
            CGFloat x = self.width;
            CGFloat y = self.height - width;
            
            for (int i = 0; i <= num; i++) {
                x -= width;
                CGContextAddArcToPoint(context, x, y, x, y + width, width);
                x -= width;
                CGContextAddLineToPoint(context, x, y + width);
                x -= width;
                CGContextAddArcToPoint(context, x + width, y, x, y, width);
            }
            
        }
            break;
            
        default:
            break;
    }
    
    CGContextClosePath(context);
    CGContextClip(context);

    CGContextFillRect(context, self.bounds);
    CGContextRestoreGState(context);

}

@end
